# 返回给前端统一数据类型
from rest_framework.response import Response


class HttpCode(object):
    ok = 200
    paramserror = 400
    unauth = 401
    methoderror = 405
    servererror = 500


def result(code=HttpCode.ok, message="", data=None, kwargs=None):
    json_dict = {"code": code, "msg": message, "data": data}
    if kwargs and isinstance(kwargs, dict) and kwargs.keys():
        json_dict.update(kwargs)
    return Response(json_dict)


# 正常返回200
def ok(message="", data=None):
    return result(message=message, data=data)


# 参数异常返回400
def params_error(message="", data=None):
    return result(code=HttpCode.paramserror, message=message, data=data)


# 需要登陆返回401
def unauth(message="", data=None):
    return result(code=HttpCode.unauth, message=message, data=data)


# 请求方法错误405
def method_error(message="", data=None):
    return result(code=HttpCode.methoderror, message=message, data=data)


# 服务器错误500
def server_error(message="", data=None):
    return result(code=HttpCode.servererror, message=message, data=data)
